package org.scau.studentevaluation.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.scau.studentevaluation.entity.dos.ActDO;
import org.scau.studentevaluation.entity.dos.ExamAct;
import org.scau.studentevaluation.entity.dos.OtherActDO;
import org.scau.studentevaluation.entity.dos.Table;
import org.scau.studentevaluation.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CollegeI implements College {
    @Autowired
    private ImportMapperService importMapperService;
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public List<Table> getTable(String college){
        //获取该学院的年度总表
        //1、查表获得所有学生学号、姓名
        //2、根据学号查表获得其所有的活动
        //3、计算出该学生的 德育总分，智育总分、体育总分，总分，然后降序排列
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        if (calendar.get(Calendar.MONTH)<10)year--;
        QueryWrapper<Table> tableQueryWrapper = new QueryWrapper<>();
        tableQueryWrapper.eq("college",college);
        List<Table> tables = studentMapper.selectList(tableQueryWrapper);
        for (Table table: tables)System.out.println(table.getName());

        for (Table table: tables){
            //根据学号查找所有的加分
            long number = table.getId();//学号
            float de = 0;
            float zhi = 0;
            float ti = 0;
            float sum = 0;
            QueryWrapper<ActDO> actDOQueryWrapper = new QueryWrapper<>();
            actDOQueryWrapper.eq("uploader_id",number);
            QueryWrapper<OtherActDO> otherActDOQueryWrapper = new QueryWrapper<>();
            otherActDOQueryWrapper.eq("student_id",number);

            List<Long> actIds = importMapperService.actMapper.selectList(actDOQueryWrapper).stream().map(ActDO::getId).collect(Collectors.toList());
            List<Long> oacIds = importMapperService.otherActMapper.selectList(otherActDOQueryWrapper).stream().map(OtherActDO::getId).collect(Collectors.toList());
//            for (Integer i : actIds)System.out.println(i);
//            for (Integer i : oacIds)System.out.println(i);
            for (Long i : actIds){//act
                ExamAct exam = importMapperService.examMapper.getExamActById(year,i);
//                System.out.println(exam.isPassed());
                if (exam!=null){//查找到当年的活动
                    int subject = exam.getSubject();
                    int reluIndex = exam.getRuleIndex();
                    float mark = exam.getMark();
                    if (exam.isPassed()){
                        if (subject == 0) de += mark;
                        else if (subject == 1)zhi += mark;
                        else if (subject == 2)ti += mark;
                    }
                }
            }
            for (Long i : oacIds){
                //根据oacId 获取
                ExamAct exam = importMapperService.examMapper.getExamActByOId(year,i);
                boolean pass = importMapperService.otherActMapper.pass(year,i);
                if (pass && exam!=null){
                    int subject = exam.getSubject();
                    int reluIndex = exam.getRuleIndex();
                    float mark = exam.getMark();
                    if (exam.isPassed()){
                        if (subject == 0) de += mark;
                        else if (subject == 1)zhi += mark;
                        else if (subject == 2)ti += mark;
                    }
                }

            }
            sum = de + zhi + ti;
            table.setDe(de);
            table.setZhi(zhi);
            table.setTi(ti);
            table.setSum(sum);
            System.out.println(table.getName() + "总分= "+ sum );
        }
//        Collections.sort(tables);
        for (int i = 0; i< tables.size()-1;i++){
            for(int j = 0;j< tables.size()-i-1;j++){
                if (tables.get(j).getSum() < tables.get(j+1).getSum()){
                    Table temp = tables.get(j);
                    tables.set(j,tables.get(j+1));
                    tables.set(j+1,temp);
                }
            }
        }
        System.out.println("-----------------------------");
        for (Table t: tables)System.out.println(t.getSum());
        return tables;
    }
}
